package com.jacobson.core.mvp

import android.app.Activity
import android.os.Bundle
import android.widget.Toast
import com.jacobson.core.base.BaseActivity
import com.jacobson.core.framework.dialog.DialogHandler
import javax.inject.Inject

/**
 * ================================================
 * 基于MVP模式需继承该Activity
 * @author Wucz on 2018/7/16 14:55
 * ================================================
 */
abstract class BaseMvpActivity<P : IPresenter> : BaseActivity(), IView {

    @Inject
    lateinit var presenter: P

    @Inject
    lateinit var dialogManager: DialogHandler

    override fun preInitData(savedInstanceState: Bundle?) {
        super.preInitData(savedInstanceState)
        presenter.onStart()
    }

    override fun visibleDialog(type: Int, args: Bundle?): Boolean {
        return dialogManager.showDialog(this, type, args)
    }

    override fun invisibleDialog(type: Int) {
        dialogManager.hideDialog(type)
    }

    override fun startLoading(type: Int, extra: Any?) {

    }

    override fun stopLoading(type: Int, extra: Any?) {

    }

    override fun onError(type: Int, extra: Any?, code: Int, message: String): Boolean = false
    override fun reload() {
        presenter.onStart()
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.onDestroy()
        invisibleDialog(-1)
    }

    override fun fetchActivity(): Activity? = this

    override fun tipToLogin() {
        Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show()
        logger.d("require Logged State, But Not!")
    }
}